package cwinsor.com.leaf_raking;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Map;

import cwinsor.com.leaf_raking.Person.Format;

abstract public class FileType {

	public abstract String getName();

	/*
	 * populate data from array of fields
	 */
	public void populate(
			ArrayList<String> theFieldsObject,
			ArrayList<Person> persons,
			Boolean supervisors) {

		doPopulate(theFieldsObject, persons, supervisors);
	}

	public abstract void doPopulate(
			ArrayList<String> theFieldsObject,
			ArrayList<Person> persons,
			Boolean supervisors);



	public void writeToFile(
			Person person,
			PrintWriter writer,
			Format format) {

		doWriteToFile(person, writer, format);
	}

	protected abstract void doWriteToFile(
			Person person,
			PrintWriter writer,
			Format format);

	public void writeToFile(
			PrintWriter writer,
			String string) {
		writer.write(string);
	}



	/*
	 * convert to a comma delimited string
	 */
	//	public void toStringCommaDelimitedAllFieldsMid(
	//			Map<Integer,String> theFieldsObject,
	//			Format fmt,
	//			Person person) {
	//
	//		doToStringCommaDelimitedAllFieldsMid(theFieldsObject, fmt, person);
	//	}

	//	public abstract void doToStringCommaDelimitedAllFieldsMid(
	//			Map<Integer,String> theFieldsObject,
	//			Format fmt,
	//			Person person);


	/*
	 * get an integer value from a string array
	 * optionally allow "" to be considered 0
	 */
	protected Integer getIntegerValueOf(
			ArrayList<String> theFieldsObject,
			int i,
			boolean convertBlankToZero) {
		Integer integer = null;

		// handle the "" case
		if (theFieldsObject.get(i).equals("") && convertBlankToZero) {
			return 0;
		}

		// all other cases
		try {
			integer = Integer.valueOf(theFieldsObject.get(i));
		} catch (NumberFormatException e) {
			Msg.fatal(dS + "NumberFormatException when converting field number " + i + " \"\"" + theFieldsObject.get(i) + " to string");
		}
		return integer;
	}


	public void applySchedulePrefernceUpdates(ArrayList<String> theFieldsObject, PrsnList personList) {

		doApplySchedulePreferenceUpdates(theFieldsObject, personList);
	}


	protected abstract void doApplySchedulePreferenceUpdates(ArrayList<String> theFieldsObject, PrsnList personList);



	// debug
	String dS = this.getClass().getSimpleName();


}
